/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accesoDatos;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import logicaNegocio.Sancion;
import oracle.jdbc.driver.OracleTypes;

/**
 *
 * @author Manuelita
 */
public class GestorSancion extends Conexion implements Gestor<Sancion> {
    private static final String ingresarSancion="";
    private static final String eliminarSancion="";
    private static final String modificarSancion="";
    private static final String listarSancion="";
    private static final String consultarSancion="";

    @Override
    public void ingresarDato(Sancion clase) {
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(ingresarSancion);
            cs.setString(1, clase.getIdentificacion());
            cs.setString(2, clase.getIdPersona());
            cs.setInt(3, clase.getEstado());
            cs.setString(4, clase.getTipoSancion());
            cs.setString(5, clase.getDescripcion());
            boolean resultado = cs.execute();
            if(resultado){
                throw null;
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(cs != null)
                    cs.close();
                Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public void eliminarDato(String identificacion) {
        CallableStatement ps = null;
        try{
            if(!this.Conectar())
                return;
            ps = conexion.prepareCall(eliminarSancion);
            ps.setString(1, identificacion);
            int resultado = ps.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(ps == null)
                    ps.close();
                this.Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public void modificarDato(Sancion clase) {
        CallableStatement ps = null;
        try{
            if(this.Conectar())
                return;
            ps = this.conexion.prepareCall(modificarSancion);
            ps.setString(1, clase.getIdentificacion());
            ps.setString(2, clase.getIdPersona());
            ps.setInt(3, clase.getEstado());
            ps.setString(4, clase.getTipoSancion());
            ps.setString(5, clase.getDescripcion());
            int resultado = ps.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
            if(ps != null)
                ps.close();
            this.Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public ArrayList<Sancion> listarDato() {
        ResultSet rs = null;
        ArrayList<Sancion> coleccion = new ArrayList<Sancion>();
        Sancion sancion = null;
        CallableStatement cs = null;
        try{
            if(!this.Conectar())
                return null;
            cs = this.conexion.prepareCall(listarSancion);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                sancion = new Sancion(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getInt(3),
                        rs.getString(4),
                        rs.getString(5)
                        );
                coleccion.add(sancion);
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                this.Desconectar();
                if(coleccion == null || coleccion.isEmpty())
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
        }
        return coleccion;
    }

    @Override
    public Sancion consultarDato(String identificacion) {
        ResultSet rs = null;
        Sancion sancion = null;
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(consultarSancion);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.setString(2, identificacion);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                sancion = new Sancion(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getInt(3),
                        rs.getString(4),
                        rs.getString(5)
                        );
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                Desconectar();
                if(sancion == null)
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getMessage());
                return null;
            }
        }
        return sancion;
    }
}
